<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html>
<head>
{include file='header.tpl' JS=$smarty.const.BAOGG_JS CSS=$smarty.const.BAOGG_CSS}        

<script type="text/javascript"> 
	var  LANG={$LANG};

	Ext.onReady(function(){
		Ext.QuickTips.init();
		
		var subject_store = Ext.create('Ext.data.TreeStore', {
			proxy : {
				type : 'ajax',
				extraParams:{ resource_type:'subject_id' },
				url : '{$resource_url}'
			},
			listeners: {
				'load':function( self,  records,  successful,  options ){
					
					Ext.getCmp('west_treePanel').expandAll();
					
				}
			}
		});

		var subject_tree = Ext.create('Ext.ux.TreePanel', {
			id : 'west_treePanel',
			store : subject_store,
			rootVisible : false,
			useArrows : true,
			 width: 250,
			region:'west',
			frame : true,
			title : '{$subject_name}',			
			//renderTo: 'subject_tree',						
			dockedItems : [ {
				xtype : 'toolbar',
				items: [{
					text : '{$LANG.expand_all}',
					iconCls:'expand-icon',
					handler: function(){
					    subject_tree.expandAll();
					}
				    }, {
					text : '{$LANG.collapse_all}',
					iconCls:'collapse-icon',
					handler: function(){
					    subject_tree.collapseAll();
					}
				    }]
			} ]
		});
		
			
    		
		var resource_store = Ext.create('Ext.data.TreeStore', {
			proxy : {
				type : 'ajax',
				extraParams:{ resource_type:'resource_id' },
				url : '{$resource_url}'
			},
			listeners: {
				'load':function( self,  records,  successful,  options ){
					//Ext.getCmp('center_treePanel').expandAll();					
                                        Ext.getCmp('center_treePanel').getRootNode().expandChildren();
				}
			}
		});

		var resource_tree = Ext.create('Ext.ux.TreePanel', {
			id : 'center_treePanel',
			store : resource_store,
			rootVisible : false,
			useArrows : true,
			 region: 'center',
			frame : true,
			title : '{$resource_name}',			
			//renderTo: 'resource_tree',						
			dockedItems : [ {
				xtype : 'toolbar',
				items : [{
					text : '{$LANG.save}',
					iconCls:'save-icon',
					handler : function() {
						var arr_subject = subject_tree.getView().getChecked(), arr_subject_id = [];
						Ext.Array.each(arr_subject, function(rec) {
							arr_subject_id.push(rec.get('id'));
						});
						
						var arr_resource = resource_tree.getView().getChecked(), arr_resource_id = [];
						Ext.Array.each(arr_resource, function(rec) {
							arr_resource_id.push(rec.get('id'));
						});

						Ext.Ajax.request({
							url: '{$edit_url}',
							method:'POST',
							params:{ subject_row_id:arr_subject_id.join(','),resource_row_id:arr_resource_id.join(',') },
							success: function(response, opts) {
							  var result = Ext.decode(response.responseText);
							  //console.dir(obj);
                                                          top.Ext.ux.ShowResult( result);
							},
							failure: function(response, opts) {
							  //console.log('server-side failure with status code ' + response.status);
							}
						});
						
					}
				},{
					text : '{$LANG.expand_all}',
					iconCls:'expand-icon',
					handler: function(){
					    resource_tree.expandAll();
					}
				    }, {
					text : '{$LANG.collapse_all}',
					iconCls:'collapse-icon',
					handler: function(){
					    resource_tree.collapseAll();
					}
				    }]
			} ]
		});
{if $object_name}
var object_store = Ext.create('Ext.data.TreeStore', {
			proxy : {
				type : 'ajax',
				extraParams:{ resource_type:'object_id' },
				url : '{$resource_url}'
			},
			listeners: {
				'load':function( self,  records,  successful,  options ){
					

					
				}
			}
		});

		var object_tree = Ext.create('Ext.ux.TreePanel', {
			id : 'west_treePanel',
			store : object_store,
			rootVisible : false,
			useArrows : true,
			region:'east',
			frame : true,
			title : '{$object_name}',			
			//renderTo: 'object_tree',						
			dockedItems : [ {
				xtype : 'toolbar',
				items : {
					text : '{$LANG.save}',
					handler : function() {
						var records = tree.getView().getChecked(), names = [];

						Ext.Array.each(records, function(rec) {

							names.push(rec.get('id'));
						});
						
						Ext.Ajax.request({
							url: '{$edit_url}',
							method:'POST',
							params:{ ids:names.join(',') },
							success: function(response, opts) {
							  var obj = Ext.decode(response.responseText);
							  console.dir(obj);
							},
							failure: function(response, opts) {
							  console.log('server-side failure with status code ' + response.status);
							}
						});
						
					}
				}
			} ]
		});
{/if}

		 /*Ext.getCmp('treePanel').getStore().getNodeById("8_list").set('checked',false)*/
		 Ext.create('Ext.container.Viewport', {
			layout: 'border',
			renderTo: Ext.getBody(),
			items: [subject_tree,resource_tree{if $object_name},object_tree{/if}]				
                });
	});
</script>

</head>
<body>
  
       <!--
<div id="subject_tree"></div>
<div id="resource_tree"></div>
<div id="object_tree"></div>
-->
</body>
</html>
